package com.fjwt.gz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.db.entity.AgentEquityEntity;
import com.fjwt.gz.db.vo.AgentEquityVO;
import com.fjwt.gz.service.mapper.AgentEquityMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 资产等级权益表 服务实现类
 * </p>
 *
 * @author shiyonghao
 * @since 2025-04-09
 */
@Slf4j
@Service
public class AgentEquityService extends ServiceImpl<AgentEquityMapper, AgentEquityEntity> {

    @Autowired
    private AgentEquityMapper agentEquityMapper;

    public List<AgentEquityVO> selectLevelEquity(Integer pid, String locationAgentNo) {
//        临时性校验代码
        if (org.apache.commons.lang3.StringUtils.isEmpty(locationAgentNo)) {
            throw new BizException("小程序已更新，请重新进入小程序！");
        }
        if (pid == null|| StringUtils.isNullOrEmpty(locationAgentNo)){
            throw new BizException("网络数据异常,请稍后重试");
        }
        return agentEquityMapper.selectLevelEquity(pid, locationAgentNo);
    }

    public List<AgentEquityVO> selectParentLevelEquity(Integer level, String locationAgentNo) {
        //        临时性校验代码
        if (org.apache.commons.lang3.StringUtils.isEmpty(locationAgentNo)) {
            throw new BizException("小程序已更新，请重新进入小程序！");
        }

        if (level == null|| StringUtils.isNullOrEmpty(locationAgentNo)){
            throw new BizException("网络数据异常,请稍后重试");
        }

        return agentEquityMapper.selectParentLevelEquity(level, locationAgentNo);
    }

    public AgentEquityVO getAssetLevelEquity(int level, String agentNo) {

        return agentEquityMapper.getAssetLevelEquity(level, agentNo);
    }

    /**
     * 查询等级权益名称
     * @param pid
     * @return
     */
    public List<AgentEquityVO> selectEquityName(Long pid, String locationAgentNo) {
        if (pid == null || ObjectUtils.isEmpty(locationAgentNo)){
            throw new BizException("网络数据异常,请稍后重试");
        }
        LambdaQueryWrapper<AgentEquityEntity> gw = AgentEquityEntity.gw().eq(AgentEquityEntity::getAgentNo, locationAgentNo).eq(AgentEquityEntity::getPid, pid).select(AgentEquityEntity::getAgentEquityId, AgentEquityEntity::getContent);
        List<AgentEquityEntity> list = this.list(gw);
        List<AgentEquityVO> voList = list.stream().map(entity -> {
            AgentEquityVO vo = BeanUtil.copyProperties(entity, AgentEquityVO.class);
            return vo;
        }).toList();
        return voList;
    }

    public List<AgentEquityEntity> selectEquityNameList(Integer pid, String agentNo) {
        if (pid == null || ObjectUtils.isEmpty(agentNo)){
            throw new BizException("网络数据异常,请稍后重试");
        }
        log.info("权益查询，权益agentEquityId：{}",pid);
        LambdaQueryWrapper<AgentEquityEntity> gw = AgentEquityEntity.gw().eq(AgentEquityEntity::getAgentNo, agentNo).eq(AgentEquityEntity::getPid, pid).select(AgentEquityEntity::getAgentEquityId, AgentEquityEntity::getContent);
        return baseMapper.selectList(gw);
    }
}
